﻿using EvertonApp.Transporte;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace EvertonApp.Data
{
   class TestComerciosRepository : IRepository<Comercio>
   {
       public static List<Comercio> _comercios = new List<Comercio>
                                                           {new Comercio(new Guid("ca761232ed4211cebacd00aa0057b223"),"Comercio1", "Valparaiso", "98642"), 
                                                            new Comercio(Guid.NewGuid(),"Comercio2", "Viña Del Mar", "23452"), 
                                                            new Comercio(Guid.NewGuid(),"Comercio3", "Santiago", "2346523"), 
                                                            new Comercio(Guid.NewGuid(),"Comercio4", "Valparaiso", "864362346684"), 
                                                            new Comercio(Guid.NewGuid(),"Comercio5", "Viña Del Mar", "423623"), 
                                                            new Comercio(Guid.NewGuid(),"Comercio6", "Santiago", "234623"), 
                                                            new Comercio(Guid.NewGuid(),"Comercio7", "Viña Del Mar", "52345"), 
                                                            new Comercio(Guid.NewGuid(),"Comercio8", "Viña Del Mar", "8568675"), 
                                                            new Comercio(Guid.NewGuid(),"Comercio9", "Viña Del Mar", "345636"), 
                                                            new Comercio(Guid.NewGuid(),"Comercio10", "Viña Del Mar", "24352345"), 
                                                            new Comercio(Guid.NewGuid(),"Comercio11", "Viña Del Mar", "456747"), 
                                                            new Comercio(Guid.NewGuid(),"Comercio12", "Viña Del Mar", "24562546"), 
                                                            new Comercio(Guid.NewGuid(),"Comercio13", "Viña Del Mar", "4657457"), 
                                                            new Comercio(Guid.NewGuid(),"Comercio14", "Viña Del Mar", "23452"), 
                                                            new Comercio(Guid.NewGuid(),"Comercio15", "Viña Del Mar", "45672"), 
                                                            new Comercio(Guid.NewGuid(),"Comercio16", "Viña Del Mar", "23634573"), 
                                                            new Comercio(Guid.NewGuid(),"Comercio17", "Viña Del Mar", "35473475"), 
                                                            new Comercio(Guid.NewGuid(),"Comercio18", "Viña Del Mar", "234525"), 
                                                            new Comercio(Guid.NewGuid(),"Comercio19", "Viña Del Mar", "347348"), 
                                                            new Comercio(Guid.NewGuid(),"Comercio20", "Viña Del Mar", "23462346"), 
                                                            new Comercio(Guid.NewGuid(),"Comercio21", "Viña Del Mar", "23452345"), 
                                                            new Comercio(Guid.NewGuid(),"Comercio22", "Viña Del Mar", "1351"), 
                                                            new Comercio(Guid.NewGuid(),"Comercio23", "Viña Del Mar", "23452435"), 
                                                            new Comercio(Guid.NewGuid(),"Comercio24", "Viña Del Mar", "2562345"), 
                                                            new Comercio(Guid.NewGuid(),"Comercio25", "Viña Del Mar", "23452"), 
                                                            new Comercio(Guid.NewGuid(),"Comercio26", "Santiago", "1341234")};

       public Comercio Insert(Comercio entity)
       {
           _comercios.Add(entity);
           entity.IdComercio = Guid.NewGuid();
           return entity;
       }

       public bool Delete(Comercio entity)
       {
           bool resultadoDelete = false;
           try
           {
               _comercios.Remove(entity);
               resultadoDelete = true;
           }catch (Exception e)
           {
               resultadoDelete = false;
           }

           return resultadoDelete;
       }

       public bool Update(Guid id, Comercio entity)
       {
           bool resultadoUpdate = false;
           try
           {
               Comercio com = _comercios.FirstOrDefault(c => c.IdComercio == id);
               if (com != null)
               {
                   com.IdComercio = entity.IdComercio;
                   com.Nombre = entity.Nombre;
                   com.Telefono = entity.Telefono;
               }
               resultadoUpdate = true;
           }catch(Exception e)
           {
               resultadoUpdate = false;
           }

           return resultadoUpdate;
       }


       public IQueryable<Comercio> SearchFor(Expression<Func<Comercio, bool>> predicate)
       {
           var collection = _comercios.AsQueryable();

           return collection.Where<Comercio>(predicate);
       }

       public IQueryable<Comercio> GetAll()
       {
           return _comercios.AsQueryable();
       }

       public Comercio GetById(Guid id)
       {
           return _comercios.Single(c => c.IdComercio == id);
       }
   }
}

